<template>
    <div id="three-bars" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
import { createPinia } from 'pinia'
import * as echarts from 'echarts/core';
import {
  ToolboxComponent,
  type ToolboxComponentOption,
  TooltipComponent,
  type TooltipComponentOption,
  GridComponent,
  type GridComponentOption,
  LegendComponent,
  type LegendComponentOption
} from 'echarts/components';
import { BarChart, type BarSeriesOption } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';
import { useChartsStore } from '@/stores/charts';
const pinia = createPinia();
const chartStore = useChartsStore(pinia)

echarts.use([
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  CanvasRenderer
]);

type EChartsOption = echarts.ComposeOption<
  | ToolboxComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | BarSeriesOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})

let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    var app: any = {};

    var chartDom = document.getElementById('three-bars')!;
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    const posList = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
    ] as const;

    app.configParameters = {
    rotate: {
        min: -90,
        max: 90
    },
    align: {
        options: {
        left: 'left',
        center: 'center',
        right: 'right'
        }
    },
    verticalAlign: {
        options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
        }
    },
    position: {
        options: posList.reduce(function (map, pos) {
        map[pos] = pos;
        return map;
        }, {} as Record<string, string>)
    },
    distance: {
        min: 0,
        max: 100
    }
    };

    app.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
        const labelOption: BarLabelOption = {
        rotate: app.config.rotate as BarLabelOption['rotate'],
        align: app.config.align as BarLabelOption['align'],
        verticalAlign: app.config
            .verticalAlign as BarLabelOption['verticalAlign'],
        position: app.config.position as BarLabelOption['position'],
        distance: app.config.distance as BarLabelOption['distance']
        };
        myChart.setOption<echarts.EChartsCoreOption>({
        series: [
            {
            label: labelOption
            },
            {
            label: labelOption
            },
            {
            label: labelOption
            },
            {
            label: labelOption
            }
        ]
        });
    }
    };

    type BarLabelOption = NonNullable<echarts.BarSeriesOption['label']>;

    const labelOption: BarLabelOption = {
        show: true,
        position: app.config.position as BarLabelOption['position'],
        distance: app.config.distance as BarLabelOption['distance'],
        align: app.config.align as BarLabelOption['align'],
        verticalAlign: app.config.verticalAlign as BarLabelOption['verticalAlign'],
        rotate: app.config.rotate as BarLabelOption['rotate'],
        formatter: '{c}',
        fontSize: 12,
        rich: {
            name: {}
        }
    };

    option = {
        color: chartStore.colorsThree,
        tooltip: {
            trigger: 'axis',
            axisPointer: {
            type: 'shadow'
            }
        },
        grid: {
            top: 0,
            left: 0,
            right: '2%',
            bottom: 0,
            containLabel: true
        },
        legend: {
            itemWidth: 10,
            itemHeight: 10,
            left: 0,
            align: 'left',
            data: props.data.seriesDataList.map(m => m.name)
        },
        toolbox: {
            show: false,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar', 'stack'] },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        xAxis: [
            {
            type: 'category',
            axisTick: { show: false },
            data: props.data.xAxisDataList,
            }
        ],
        yAxis: [
            {
                type: 'value',
                axisLabel: {
                    show: true,
                    color: '#c8c8c8',
                    formatter: function (value) { return value+'K' }
                }
            }
        ],
        series: props.data.seriesDataList.map(m => {
            m['type'] = 'bar'
            m['label'] = labelOption
            m['emphasis'] = { focus: 'series' }
            return m
        })
    };

    option && myChart.setOption(option);

})
</script>

<style scope>
</style>
